﻿using Microsoft.AspNetCore.Mvc;
using HttpServer.Models;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using static HttpServer.Models.UserProfileData;
using System;
namespace HttpServer.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class UserProfileController : ControllerBase
    {
        ///// <summary>
        ///// 查
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet("GetAllUser")]
        //public ActionResult<List<UserProfileData>> Get()
        //{
        //    using (MySqlConnection connection = new MySqlConnection(SqlLinkManager.sqlConnectionCommand))
        //    {
        //        try
        //        {
        //            connection.Open();
        //            Console.WriteLine(SqlLinkManager.GetSearchCommand("userprofile"));
        //            using (MySqlCommand command = new MySqlCommand(SqlLinkManager.GetSearchCommand("userProfile"), connection))
        //            {
        //                List<UserProfileData> userProfileDatas = new List<UserProfileData>();
        //                MySqlDataReader dr = command.ExecuteReader();
        //                while (dr.Read())
        //                {
        //                    for (int i = 0; i < dr.FieldCount; i++)
        //                    {
        //                        Console.WriteLine(dr[i].ToString());
        //                    }
        //                    UserProfileData userProfileData = new UserProfileData();
        //                    userProfileData.userId = dr["userId"] == DBNull.Value ? -1 : (int)dr["userid"];
        //                    userProfileData.nickName = dr["nickName"] == DBNull.Value ? "" : (string)dr["nickName"];
        //                    userProfileData.avatarUrl = dr["avatarUrl"] == DBNull.Value ? "" : (string)dr["avatarUrl"];
        //                    userProfileData.gender = dr["gender"] == DBNull.Value ? Gender.None : Enum.Parse<Gender>(dr["gender"].ToString());
        //                    userProfileData.accountNumber = dr["accountNumber"] == DBNull.Value ? "" : (string)dr["accountNumber"];
        //                    userProfileDatas.Add(userProfileData);
        //                }
        //                userProfileDatas[0].userId = 100;
        //                return StatusCode(200, userProfileDatas);
        //            }
        //        }
        //        catch (Exception e)
        //        {
        //            Console.WriteLine("连接失败:" + e);
        //        }
        //    }
        //    return StatusCode(400, "Error");
        //}
        /// <summary>
        /// 查找用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetUser/{id}")]
        public ActionResult<UserProfileData> Get(int id)
        {
            using (MySqlConnection connection = new MySqlConnection(SqlLinkManager.sqlConnectionCommand))
            {
                try
                {
                    connection.Open();
                    using (MySqlCommand command = new MySqlCommand(SqlLinkManager.GetSearchCommand("userprofile", "userId", id), connection))
                    {
                        UserProfileData userProfileData = new UserProfileData();
                        MySqlDataReader dr = command.ExecuteReader();
                        while (dr.Read())
                        {
                            userProfileData.userId = dr["userId"] == DBNull.Value ? -1 : (int)dr["userid"];
                            userProfileData.nickName = dr["nickName"] == DBNull.Value ? "" : (string)dr["nickName"];
                            userProfileData.avatarUrl = dr["avatarUrl"] == DBNull.Value ? "" : (string)dr["avatarUrl"];
                            userProfileData.gender = dr["gender"] == DBNull.Value ? Gender.None : Enum.Parse<Gender>(dr["gender"].ToString());
                            userProfileData.accountNumber = dr["accountNumber"] == DBNull.Value ? "" : (string)dr["accountNumber"];
                        }
                        if (!string.IsNullOrEmpty(userProfileData.accountNumber))
                        {
                            return StatusCode(200,new RequestData(200,"",userProfileData));
                        }
                        else
                        {
                            return StatusCode(200, null);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("连接失败:" + e);
                }
                finally
                {
                    connection.Close();
                }
            }
            return StatusCode(400, "Error");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpPost("DailySportSignIn")]
        public IActionResult DailySportSignIn([FromQuery] string userId)
        {
            string nowTimeStamp = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            //SqlLinkManager.GetData()
            //SqlLinkManager.InsertData("dailysportsignin",)
            return StatusCode(400, "Error");
        }
    }
}
